

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>pysimm.forcefield.forcefield &mdash; pysimm 0.2 documentation</title>
  

  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
    <link rel="top" title="pysimm 0.2 documentation" href="../../../index.html"/>
        <link rel="up" title="pysimm" href="../../pysimm.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> pysimm
          

          
          </a>

          
            
            
              <div class="version">
                0.2
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
                <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../pysimm.html">pysimm package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../../../index.html">pysimm</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          





<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../../../index.html">Docs</a> &raquo;</li>
      
          <li><a href="../../index.html">Module code</a> &raquo;</li>
      
          <li><a href="../../pysimm.html">pysimm</a> &raquo;</li>
      
    <li>pysimm.forcefield.forcefield</li>
      <li class="wy-breadcrumbs-aside">
        
          
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for pysimm.forcefield.forcefield</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># pysimm.forcefield module</span>
<span class="c1"># ******************************************************************************</span>
<span class="c1">#</span>
<span class="c1"># ******************************************************************************</span>
<span class="c1"># License</span>
<span class="c1"># ******************************************************************************</span>
<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016 Michael E. Fortunato, Coray M. Colina</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in</span>
<span class="c1"># all copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN</span>
<span class="c1"># THE SOFTWARE.</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">from</span> <span class="nn">xml.dom</span> <span class="kn">import</span> <span class="n">minidom</span>
<span class="kn">from</span> <span class="nn">xml.etree</span> <span class="kn">import</span> <span class="n">ElementTree</span> <span class="k">as</span> <span class="n">Et</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">permutations</span><span class="p">,</span> <span class="n">combinations</span>

<span class="kn">from</span> <span class="nn">pysimm</span> <span class="kn">import</span> <span class="n">error_print</span>
<span class="kn">from</span> <span class="nn">pysimm</span> <span class="kn">import</span> <span class="n">warning_print</span>
<span class="kn">from</span> <span class="nn">pysimm</span> <span class="kn">import</span> <span class="n">debug_print</span>
<span class="kn">import</span> <span class="nn">gasteiger</span>
<span class="kn">from</span> <span class="nn">pysimm.utils</span> <span class="kn">import</span> <span class="n">PysimmError</span><span class="p">,</span> <span class="n">Item</span><span class="p">,</span> <span class="n">ItemContainer</span><span class="p">,</span> <span class="n">compare</span>
<span class="kn">from</span> <span class="nn">pysimm.system</span> <span class="kn">import</span> <span class="n">ParticleType</span><span class="p">,</span> <span class="n">BondType</span><span class="p">,</span> <span class="n">AngleType</span>
<span class="kn">from</span> <span class="nn">pysimm.system</span> <span class="kn">import</span> <span class="n">Angle</span><span class="p">,</span> <span class="n">Dihedral</span><span class="p">,</span> <span class="n">Improper</span>
<span class="kn">from</span> <span class="nn">pysimm.system</span> <span class="kn">import</span> <span class="n">DihedralType</span><span class="p">,</span> <span class="n">ImproperType</span>


<span class="n">element_names_by_mass</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">:</span> <span class="s1">&#39;H&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">:</span> <span class="s1">&#39;He&#39;</span><span class="p">,</span> <span class="mi">7</span><span class="p">:</span> <span class="s1">&#39;Li&#39;</span><span class="p">,</span> <span class="mi">9</span><span class="p">:</span> <span class="s1">&#39;Be&#39;</span><span class="p">,</span> <span class="mi">11</span><span class="p">:</span> <span class="s1">&#39;B&#39;</span><span class="p">,</span> <span class="mi">12</span><span class="p">:</span> <span class="s1">&#39;C&#39;</span><span class="p">,</span>
                         <span class="mi">14</span><span class="p">:</span> <span class="s1">&#39;N&#39;</span><span class="p">,</span> <span class="mi">16</span><span class="p">:</span> <span class="s1">&#39;O&#39;</span><span class="p">,</span> <span class="mi">19</span><span class="p">:</span> <span class="s1">&#39;F&#39;</span><span class="p">,</span> <span class="mi">20</span><span class="p">:</span> <span class="s1">&#39;Ne&#39;</span><span class="p">,</span> <span class="mi">23</span><span class="p">:</span> <span class="s1">&#39;Na&#39;</span><span class="p">,</span>
                         <span class="mi">24</span><span class="p">:</span> <span class="s1">&#39;Mg&#39;</span><span class="p">,</span> <span class="mi">27</span><span class="p">:</span> <span class="s1">&#39;Al&#39;</span><span class="p">,</span> <span class="mi">28</span><span class="p">:</span> <span class="s1">&#39;Si&#39;</span><span class="p">,</span> <span class="mi">31</span><span class="p">:</span> <span class="s1">&#39;P&#39;</span><span class="p">,</span> <span class="mi">32</span><span class="p">:</span> <span class="s1">&#39;S&#39;</span><span class="p">,</span>
                         <span class="mi">35</span><span class="p">:</span> <span class="s1">&#39;Cl&#39;</span><span class="p">,</span> <span class="mi">39</span><span class="p">:</span> <span class="s1">&#39;K&#39;</span><span class="p">,</span> <span class="mi">40</span><span class="p">:</span> <span class="s1">&#39;Ca&#39;</span><span class="p">,</span> <span class="mi">80</span><span class="p">:</span> <span class="s1">&#39;Br&#39;</span><span class="p">,</span> <span class="mi">127</span><span class="p">:</span> <span class="s1">&#39;I&#39;</span><span class="p">}</span>


<div class="viewcode-block" id="Forcefield"><a class="viewcode-back" href="../../../pysimm.forcefield.forcefield.html#pysimm.forcefield.forcefield.Forcefield">[docs]</a><span class="k">class</span> <span class="nc">Forcefield</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;pysimm.forcefield.Forcefield</span>

<span class="sd">    Base Forcefield class definition.</span>
<span class="sd">    Initialize with force field xml file.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        ff_class: force field class (1 or 2)</span>
<span class="sd">        ff_name: force field name</span>
<span class="sd">        particle_types: :class:`~pysimm.utils.ItemContainer` for particle_types</span>
<span class="sd">        bond_types: :class:`~pysimm.utils.ItemContainer` for bond_types</span>
<span class="sd">        angle_types: :class:`~pysimm.utils.ItemContainer` for angle_types</span>
<span class="sd">        dihedral_types: :class:`~pysimm.utils.ItemContainer` for dihedral_types</span>
<span class="sd">        improper_types: :class:`~pysimm.utils.ItemContainer` for improper_types</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">format</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ff_class</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ff_name</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pair_style</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">particle_types</span> <span class="o">=</span> <span class="n">ItemContainer</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bond_style</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bond_types</span> <span class="o">=</span> <span class="n">ItemContainer</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">angle_style</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">angle_types</span> <span class="o">=</span> <span class="n">ItemContainer</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dihedral_style</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dihedral_types</span> <span class="o">=</span> <span class="n">ItemContainer</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">improper_style</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">improper_types</span> <span class="o">=</span> <span class="n">ItemContainer</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">file_</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">format</span> <span class="o">==</span> <span class="s1">&#39;json&#39;</span> <span class="ow">or</span> <span class="n">file_</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;json&#39;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">from_json</span><span class="p">(</span><span class="n">file_</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">format</span> <span class="o">==</span> <span class="s1">&#39;xml&#39;</span> <span class="ow">or</span> <span class="n">file_</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;xml&#39;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">from_xml</span><span class="p">(</span><span class="n">file_</span><span class="p">)</span>
            
    
<div class="viewcode-block" id="Forcefield.from_xml"><a class="viewcode-back" href="../../../pysimm.forcefield.forcefield.html#pysimm.forcefield.forcefield.Forcefield.from_xml">[docs]</a>    <span class="k">def</span> <span class="nf">from_xml</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_</span><span class="p">):</span>
        <span class="n">tree</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">file_</span><span class="p">)</span>
        <span class="n">root</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">getroot</span><span class="p">()</span>
        <span class="n">ptypes</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;ParticleTypes&#39;</span><span class="p">)</span>
        <span class="n">btypes</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;BondTypes&#39;</span><span class="p">)</span>
        <span class="n">atypes</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;AngleTypes&#39;</span><span class="p">)</span>
        <span class="n">dtypes</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;DihedralTypes&#39;</span><span class="p">)</span>
        <span class="n">itypes</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;ImproperTypes&#39;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">ptype</span> <span class="ow">in</span> <span class="n">ptypes</span><span class="p">:</span>
            <span class="n">pt</span> <span class="o">=</span> <span class="n">ParticleType</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">ptype</span><span class="o">.</span><span class="n">attrib</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">v</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">pt</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
            <span class="n">pt</span><span class="o">.</span><span class="n">tag</span> <span class="o">=</span> <span class="n">pt</span><span class="o">.</span><span class="n">name</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">particle_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">btype</span> <span class="ow">in</span> <span class="n">btypes</span><span class="p">:</span>
            <span class="n">bt</span> <span class="o">=</span> <span class="n">BondType</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">btype</span><span class="o">.</span><span class="n">attrib</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">v</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">bt</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
            <span class="n">bt</span><span class="o">.</span><span class="n">tag</span> <span class="o">=</span> <span class="n">bt</span><span class="o">.</span><span class="n">name</span>
            <span class="n">bt</span><span class="o">.</span><span class="n">rname</span> <span class="o">=</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">bt</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bond_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">bt</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">atype</span> <span class="ow">in</span> <span class="n">atypes</span><span class="p">:</span>
            <span class="n">at</span> <span class="o">=</span> <span class="n">AngleType</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">atype</span><span class="o">.</span><span class="n">attrib</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">v</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
            <span class="n">at</span><span class="o">.</span><span class="n">tag</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">name</span>
            <span class="n">at</span><span class="o">.</span><span class="n">rname</span> <span class="o">=</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">angle_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">at</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">dtype</span> <span class="ow">in</span> <span class="n">dtypes</span><span class="p">:</span>
            <span class="n">dt</span> <span class="o">=</span> <span class="n">DihedralType</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">dtype</span><span class="o">.</span><span class="n">attrib</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">k</span> <span class="o">!=</span> <span class="s1">&#39;d&#39;</span> <span class="ow">and</span> <span class="n">k</span> <span class="o">!=</span> <span class="s1">&#39;n&#39;</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">v</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                        <span class="k">pass</span>
                <span class="k">elif</span> <span class="n">k</span> <span class="o">==</span> <span class="s1">&#39;d&#39;</span> <span class="ow">or</span> <span class="n">k</span> <span class="o">==</span> <span class="s1">&#39;n&#39;</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">v</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                        <span class="k">pass</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
            <span class="n">dt</span><span class="o">.</span><span class="n">tag</span> <span class="o">=</span> <span class="n">dt</span><span class="o">.</span><span class="n">name</span>
            <span class="n">dt</span><span class="o">.</span><span class="n">rname</span> <span class="o">=</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">dt</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dihedral_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">itype</span> <span class="ow">in</span> <span class="n">itypes</span><span class="p">:</span>
            <span class="n">it</span> <span class="o">=</span> <span class="n">ImproperType</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">itype</span><span class="o">.</span><span class="n">attrib</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">v</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">it</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
            <span class="n">it</span><span class="o">.</span><span class="n">tag</span> <span class="o">=</span> <span class="n">it</span><span class="o">.</span><span class="n">name</span>
            <span class="n">it</span><span class="o">.</span><span class="n">rname</span> <span class="o">=</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">it</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">improper_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">it</span><span class="p">)</span></div>
    
    
<div class="viewcode-block" id="Forcefield.from_json"><a class="viewcode-back" href="../../../pysimm.forcefield.forcefield.html#pysimm.forcefield.forcefield.Forcefield.from_json">[docs]</a>    <span class="k">def</span> <span class="nf">from_json</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">json_file</span><span class="p">):</span>
        <span class="k">with</span> <span class="nb">file</span><span class="p">(</span><span class="n">json_file</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">j</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ff_name</span> <span class="o">=</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;ff_name&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ff_class</span> <span class="o">=</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;ff_class&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pair_style</span> <span class="o">=</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;pair_style&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bond_style</span> <span class="o">=</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;bond_style&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">angle_style</span> <span class="o">=</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;angle_style&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dihedral_style</span> <span class="o">=</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;dihedral_style&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">improper_style</span> <span class="o">=</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;improper_style&#39;</span><span class="p">)</span>
        
        <span class="k">for</span> <span class="n">pt</span> <span class="ow">in</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;particle_types&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">particle_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ParticleType</span><span class="p">(</span><span class="o">**</span><span class="n">pt</span><span class="p">))</span>
        
        <span class="k">for</span> <span class="n">bt</span> <span class="ow">in</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;bond_types&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bond_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">BondType</span><span class="p">(</span><span class="o">**</span><span class="n">bt</span><span class="p">))</span>
        
        <span class="k">for</span> <span class="n">at</span> <span class="ow">in</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;angle_types&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">angle_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">AngleType</span><span class="p">(</span><span class="o">**</span><span class="n">at</span><span class="p">))</span>
        
        <span class="k">for</span> <span class="n">dt</span> <span class="ow">in</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;dihedral_types&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dihedral_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">DihedralType</span><span class="p">(</span><span class="o">**</span><span class="n">dt</span><span class="p">))</span>
        
        <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">j</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;improper_types&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">improper_types</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ImproperType</span><span class="p">(</span><span class="o">**</span><span class="n">it</span><span class="p">))</span></div>
        
            
<div class="viewcode-block" id="Forcefield.write_json"><a class="viewcode-back" href="../../../pysimm.forcefield.forcefield.html#pysimm.forcefield.forcefield.Forcefield.write_json">[docs]</a>    <span class="k">def</span> <span class="nf">write_json</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">out</span><span class="p">):</span>
        <span class="n">f</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;ff_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ff_name</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;ff_class&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ff_class</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;pair_style&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pair_style</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;bond_style&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bond_style</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;angle_style&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">angle_style</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;dihedral_style&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dihedral_style</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;improper_style&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">improper_style</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;particle_types&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nb">vars</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span> <span class="k">for</span> <span class="n">pt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">particle_types</span><span class="p">]</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;bond_types&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nb">vars</span><span class="p">(</span><span class="n">bt</span><span class="p">)</span> <span class="k">for</span> <span class="n">bt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">bond_types</span><span class="p">]</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;angle_types&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nb">vars</span><span class="p">(</span><span class="n">at</span><span class="p">)</span> <span class="k">for</span> <span class="n">at</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">angle_types</span><span class="p">]</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;dihedral_types&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nb">vars</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="k">for</span> <span class="n">dt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">dihedral_types</span><span class="p">]</span>
        <span class="n">f</span><span class="p">[</span><span class="s1">&#39;improper_types&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nb">vars</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">improper_types</span><span class="p">]</span>
        <span class="k">with</span> <span class="nb">file</span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">_file</span><span class="p">:</span>
            <span class="n">_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span></div>
            

<div class="viewcode-block" id="Forcefield.write_xml"><a class="viewcode-back" href="../../../pysimm.forcefield.forcefield.html#pysimm.forcefield.forcefield.Forcefield.write_xml">[docs]</a>    <span class="k">def</span> <span class="nf">write_xml</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">out</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;pysimm.forcefield.Forcefield.write</span>

<span class="sd">        Write Forcefield object to xml format.</span>

<span class="sd">        Args:</span>
<span class="sd">            out: file name to write</span>

<span class="sd">        Returns:</span>
<span class="sd">            None</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">ff_elem</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">Element</span><span class="p">(</span><span class="s1">&#39;Forcefield&#39;</span><span class="p">)</span>
        <span class="n">ff_elem</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ff_name</span><span class="p">)</span>
        <span class="n">ff_elem</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s1">&#39;class&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ff_class</span><span class="p">)</span>

        <span class="n">ptypes</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">ff_elem</span><span class="p">,</span> <span class="s1">&#39;ParticleTypes&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">pt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">particle_types</span><span class="p">:</span>
            <span class="n">ptype</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">ptypes</span><span class="p">,</span> <span class="s1">&#39;Type&#39;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">vars</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">ptype</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>

        <span class="n">btypes</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">ff_elem</span><span class="p">,</span> <span class="s1">&#39;BondTypes&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">bt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">bond_types</span><span class="p">:</span>
            <span class="n">btype</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">btypes</span><span class="p">,</span> <span class="s1">&#39;Type&#39;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">vars</span><span class="p">(</span><span class="n">bt</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">btype</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>

        <span class="n">atypes</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">ff_elem</span><span class="p">,</span> <span class="s1">&#39;AngleTypes&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">at</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">angle_types</span><span class="p">:</span>
            <span class="n">atype</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">atypes</span><span class="p">,</span> <span class="s1">&#39;Type&#39;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">vars</span><span class="p">(</span><span class="n">at</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">atype</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>

        <span class="n">dtypes</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">ff_elem</span><span class="p">,</span> <span class="s1">&#39;DihedralTypes&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">dt</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">dihedral_types</span><span class="p">:</span>
            <span class="n">dtype</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">dtypes</span><span class="p">,</span> <span class="s1">&#39;Type&#39;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">vars</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">dtype</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>

        <span class="n">itypes</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">ff_elem</span><span class="p">,</span> <span class="s1">&#39;ImproperTypes&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">improper_types</span><span class="p">:</span>
            <span class="n">itype</span> <span class="o">=</span> <span class="n">Et</span><span class="o">.</span><span class="n">SubElement</span><span class="p">(</span><span class="n">itypes</span><span class="p">,</span> <span class="s1">&#39;Type&#39;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">vars</span><span class="p">(</span><span class="n">it</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">itype</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">))</span>

        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span>
                <span class="n">minidom</span><span class="o">.</span><span class="n">parseString</span><span class="p">(</span><span class="n">Et</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">ff_elem</span><span class="p">,</span> <span class="s1">&#39;utf-8&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">toprettyxml</span><span class="p">(</span>
                    <span class="n">indent</span><span class="o">=</span><span class="s2">&quot;  &quot;</span><span class="p">))</span></div></div>
</pre></div>

           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2017, https://pysimm.org.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'0.2',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>